package cn.t.repository.impl;

import cn.t.converter.IndustryUserInfoConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.IndustryUserInfoDO;
import cn.t.persistence.entity.IndustryUserInfoPO;
import cn.t.persistence.mapper.IndustryUserInfoMapper;
import cn.t.repository.IndustryUserInfoRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Slf4j
@Repository
@AllArgsConstructor
public class IndustryUserInfoRepositoryImpl extends RepositoryImpl<IndustryUserInfoMapper, IndustryUserInfoPO, IndustryUserInfoConverter, IndustryUserInfoDO> implements IndustryUserInfoRepository {


    @Override
    public List<IndustryUserInfoDO> findByModel(IndustryUserInfoDO industryUserInfoDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<IndustryUserInfoPO> list = getWrapper(industryUserInfoDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<IndustryUserInfoPO> getWrapper(IndustryUserInfoDO industryUserInfoDO) {
        LambdaQueryChainWrapper<IndustryUserInfoPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getPhoneNumber())) {
            wrapper.like(IndustryUserInfoPO::getPhoneNumber, industryUserInfoDO.getPhoneNumber());
        }
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getFirmName())) {
            wrapper.eq(IndustryUserInfoPO::getFirmName, industryUserInfoDO.getFirmName());
        }
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getDept())) {
            wrapper.eq(IndustryUserInfoPO::getDept, industryUserInfoDO.getDept());
        }
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getJob())) {
            wrapper.eq(IndustryUserInfoPO::getJob, industryUserInfoDO.getJob());
        }
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getSkuCode())) {
            wrapper.eq(IndustryUserInfoPO::getSkuCode, industryUserInfoDO.getSkuCode());
        }
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getQty())) {
            wrapper.eq(IndustryUserInfoPO::getQty, industryUserInfoDO.getQty());
        }
        if (!ObjectUtils.isEmpty(industryUserInfoDO.getDeviceId())) {
            wrapper.eq(IndustryUserInfoPO::getDeviceId, industryUserInfoDO.getDeviceId());
        }
        return wrapper;
    }

    @Override
    public long count(IndustryUserInfoDO industryUserInfoDO) {
        return getWrapper(industryUserInfoDO).count();
    }
}
